﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using Nop.Admin.Models.ShowCase;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.ShowCase;
using Nop.Core.Infrastructure;
using Nop.Services.Catalog;
using Nop.Services.Media;
using Nop.Services.Seo;
using Nop.Services.ShowCase;

namespace Nop.Admin.Controllers
{
    public class ShowCaseApiResult
    {
        public bool Success { get; set; }
        public string Msg { get; set; }
    }

    public class ProductInfo
    {
         public int ProductId{ get; set; }

        public string ProductUrl { get; set; }

        public string ProductPictureUrl{ get; set; }

        public string ProductName { get; set; }

    }

    public class ShowCaseApiController : ApiController
    {

        public IShowCaseService _showCaseService { get; set; }

        public IProductService _productService { get; set; }

        public IPictureService _pictureService { get; set; }

        public IUrlRecordService _UrlRecordService { get; set; }

        public ShowCaseApiController()
        {
            _showCaseService = EngineContext.Current.Resolve<IShowCaseService>();
            _productService =  EngineContext.Current.Resolve<IProductService>();
            _pictureService =   EngineContext.Current.Resolve<IPictureService>();
            _UrlRecordService =   EngineContext.Current.Resolve<IUrlRecordService>();
        }

        

        public ShowCaseEditModel Get(int showCaseId)
        {

            var showCaseProduct = _showCaseService.GetShowCaseProduct(showCaseId);


            var model = new ShowCaseEditModel()
                {
                    Id = showCaseProduct.Id,
                    Title = showCaseProduct.Title,
                    ShowCaseListItemProductModels = showCaseProduct.Items.Select(t=>new ShowCaseEditItemModel()
                        {
                            ProductId = t.Product.Id,
                            ProductName = t.Product.Name,
                            ProductPictureUrl = GetProductPictureUrl(t.Product),
                            ProductUrl = GetProductUrl(t.Product),
                            Sort = t.Sort

                        }).ToList()

                };
            return model;
        }

        private string GetProductUrl(Product product)
        {
            var u = new UrlHelper(HttpContext.Current.Request.RequestContext);
            var seName = product.GetSeName();
            if (!string.IsNullOrEmpty(seName))
            {

                var url = u.RouteUrl("Product", new {SeName = seName});
                return url;
            }
            return string.Empty;
        }

        private string GetProductPictureUrl(Product p)
        {
            var picture = p.GetDefaultProductPicture(_pictureService);
            return _pictureService.GetPictureUrl(picture, 140);
        }
        

        public ProductInfo GetProduct(string productUrl)
        {
            try
            {

                var lastIndex = productUrl.LastIndexOf("/");
                var seName = productUrl.Substring(lastIndex+1);
                var urlRecord = _UrlRecordService.GetBySlug(seName);

                if (urlRecord == null)
                {
                    throw new Exception(string.Format("url record not exist for producturl{0}", productUrl));
                }

                var productId = urlRecord.EntityId;
                var p = _productService.GetProductById(productId);
                var result = new ProductInfo()
                    {
                        ProductId = p.Id,
                        ProductName = p.Name,
                        ProductPictureUrl = this.GetProductPictureUrl(p),
                        ProductUrl = productUrl
                    };


                return result;

            }
            catch (Exception e)
            {
                return null;
            }
        }

        public ShowCaseApiResult Save(ShowCaseEditModel model)
        {
            try
            {
                var p = new ShowCaseProduct()
                            {
                                Title = model.Title,
                                Id = model.Id,
                            };
                if (model.ShowCaseListItemProductModels != null && model.ShowCaseListItemProductModels.Count > 0)
                {
                    var showCaseItems = model.ShowCaseListItemProductModels.Select(t => new ShowCaseProductItem()
                                                                                            {
                                                                                                Product =
                                                                                                    _productService.
                                                                                                    GetProductById(
                                                                                                        t.ProductId),
                                                                                                Sort = t.Sort,
                                                                                                ShowCaseProduct = p,

                                                                                            }).ToList();

                    
                    p.Items = showCaseItems;
                }


                _showCaseService.SaveShowCase(p);

                return new ShowCaseApiResult()
                           {
                               Success = true,
                           };
            }
            catch (Exception e)
            {

                return new ShowCaseApiResult()
                           {
                               Success = false,
                               Msg = e.ToString()
                           };
            }

        }


        //// POST api/showcaseapi
        //public void Post([FromBody]string value)
        //{
        //}

        //// PUT api/showcaseapi/5
        //public void Put(int id, [FromBody]string value)
        //{
        //}

        //// DELETE api/showcaseapi/5
        //public void Delete(int id)
        //{
        //}
    }
}
